/*
 * Copyright (C) 2012 Atlas of Living Australia
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 */


import grails.converters.JSON
import grails.converters.XML

class RenderFilters {

    def filters = {
        multiFormat(controller: 'tree', action: '*', find: true) {
            after = { Map model ->
                def out = model?.containsKey('out')?model.out:model
                if (response.format == "json" && params.callback) {
                    render(text: params.callback + "(${out as JSON})" , contentType: 'application/javascript', encoding:"UTF-8")
                    false
                } else if (response.format == "json") {
                    render(text: out as JSON, contentType: 'application/json', encoding:"UTF-8")
                    false
                } else if (response.format == "xml") {
                    render(text: out as XML, contentType: 'application/xml', encoding:"UTF-8")
                    false
                }
            }
        }
    }
}